/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spatialmarketplace.cem.ext;

import net.sourceforge.stripes.action.ActionBeanContext;
import spatialmarketplace.cem.model.User;
import net.sourceforge.stripes.util.Log;
import spatialmarketplace.cem.dao.implementation.UserDaoImpl;

/**
 * This class contains the User information to be accessed from all the JSPs and Action Beans of the application.
 * @author Camila
 */
public class CEMActionBeanContext extends ActionBeanContext {

    private static final Log logger = Log.getInstance(CEMActionBeanContext.class);
    private static final Integer DEFAULT_USER_ID = new Integer(2);
    private static Integer userId = null;
    private static String userName;
    private static User user;
    private static Integer groupOrResourceId;
    private static Boolean forumBelongsToGroup;

    /**
     * Returns the userName of the current user. If there is no user set it chooses the default User.
     * @return userName
     */
    public String getUserName() {
        if (user == null) {
            getCurrentUser();
        }

        return userName;
    }

    /**
     * Returns the userId of the current user. If there is no user set it chooses the default User.
     * @return userId
     */
    public Integer getUserId() {
        if (user == null) {
            getCurrentUser();
        }
        
        return userId;
    }

    /**
     * Updates the current user to the userId received.
     * @param usrId 
     */
    public void setCurrentUser(Integer usrId) {
        try {
            user = UserDaoImpl.getInstance().readUser(usrId);
            userId = user.getUserId();
            userName = user.getUserName();

        } catch (Exception e) {
            logger.error("Error" + e);
            System.out.println("Error: " + CEMActionBeanContext.class.getName() + ", " + e);

            //If there is an error retrieving the user, then try to initialize it with the default User ID
            try {
                user = UserDaoImpl.getInstance().readUser(2);
                userId = user.getUserId();
                userName = user.getUserName();
            } catch (Exception ex) {
                logger.error("Error" + ex);
                System.out.println("Error: " + CEMActionBeanContext.class.getName() + ", " + ex);
                
                //If it fails even with the default user ID at least initialize the header details by default.
                userId = DEFAULT_USER_ID;
                userName = "Torab Torabi";

            }
        }
    }

    /**
     * Returns the current User as an object from the model.
     * @return user
     */
    public User getCurrentUser() {
        if (userId == null){
            userId = DEFAULT_USER_ID;
        }
        try {
            user = UserDaoImpl.getInstance().readUser(userId);
            userId = user.getUserId();
            userName = user.getUserName();

        } catch (Exception e) {
            logger.error("Error" + e);
            System.out.println("Error: " + CEMActionBeanContext.class.getName() + ", " + e);
        };
        return user;
    }

    public Boolean getForumBelongsToGroup() {
        return forumBelongsToGroup;
    }

    public void setForumBelongsToGroup(Boolean forumBelongsToGroup) {
        CEMActionBeanContext.forumBelongsToGroup = forumBelongsToGroup;
    }

    public Integer getGroupOrResourceId() {
        return groupOrResourceId;
    }

    public void setGroupOrResourceId(Integer groupOrResourceId) {
        CEMActionBeanContext.groupOrResourceId = groupOrResourceId;
    }
    
    
}
